package com.jinsp.webviewservice.webview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.jinsp.webviewservice.util.NetUtils;

/**
 * Created by luoshuai on 12/30/21
 * <p>
 * WebView默认配置信息
 */
public class DefaultWebViewSettings {

    private static volatile DefaultWebViewSettings INSTANCE;

    private DefaultWebViewSettings() {
    }

    public static DefaultWebViewSettings getInstance() {
        if (INSTANCE == null) {
            synchronized (DefaultWebViewSettings.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DefaultWebViewSettings();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 初始化WebView设置参数
     *
     * @param webView WebView对象
     */
    @SuppressLint("SetJavaScriptEnabled")
    public void initWebViewSettings(WebView webView) {
        if (webView != null) {
            //配置参数
            WebSettings webSettings = webView.getSettings();
            webSettings.setTextZoom(100);
            webSettings.setJavaScriptEnabled(true);
            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webSettings.setSupportZoom(true);
            webSettings.setBuiltInZoomControls(false);
            webSettings.setDatabaseEnabled(true);
            webSettings.setAppCacheEnabled(true);
            webSettings.setLoadsImagesAutomatically(true);
            webSettings.setSupportMultipleWindows(false);
            webSettings.setCacheMode(NetUtils.isNetworkConnected(webView.getContext()) ? WebSettings.LOAD_NO_CACHE
                    : WebSettings.LOAD_CACHE_ELSE_NETWORK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                WebView.enableSlowWholeDocumentDraw();
                WebView.setWebContentsDebuggingEnabled(true);
                webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            webSettings.setBlockNetworkImage(false);
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(false);
            webSettings.setAllowUniversalAccessFromFileURLs(false);
//            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//            webSettings.setSavePassword(false);
            webSettings.setSaveFormData(false);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setUseWideViewPort(true);
            webSettings.setDomStorageEnabled(true);
            webSettings.setNeedInitialFocus(true);
            webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
            webSettings.setDefaultFontSize(16);
            webSettings.setMinimumFontSize(10);
            webSettings.setGeolocationEnabled(true);
            String appCacheDir = webView.getContext().getDir("cache", Context.MODE_PRIVATE).getPath();
//            webSettings.setDatabasePath(appCacheDir);
            webSettings.setAppCachePath(appCacheDir);
//            webSettings.setAppCacheMaxSize(1024*1024*80);
        }
    }
}
